package com.example.dialog.Activity;

import static java.security.AccessController.getContext;

import android.content.Intent;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.example.dialog.R;
import com.example.dialog.Tools.ArticleInfo;
import com.example.dialog.Tools.DatabaseHelperForSearchDetails;
import com.example.dialog.Tools.DatabaseHelperForSearchResult;
import com.example.dialog.Tools.MyImageView;

import java.util.ArrayList;
import java.util.List;

public class SearchResultsActivity extends AppCompatActivity {
    private ImageView iv_back;
    private EditText et_search_text;
    private Button btn_search;
    private ListView lv_search_results;

    private List<String> searchResults;//搜索结果列表
    private ArrayAdapter<String> adapter;//适配器

    private RecyclerView rv_search_details;
    private MyAdapter mMyAdapter;
    List<ArticleInfo> searchDetails;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_search_details);

        this.iv_back = findViewById(R.id.iv_back); //返回导航

        this.et_search_text = findViewById(R.id.et_search_text); //搜索输入框
        Intent intent = getIntent();
        String data = intent.getStringExtra("et_search_text");
        this.et_search_text.setText(data);//设置搜索框的内容为搜索时传入的

        this.btn_search = findViewById(R.id.btn_search); //搜索按钮
        this.lv_search_results = findViewById(R.id.lv_search_results); //搜索匹配结果

        iv_back.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                //返回首页
                Intent intent=new Intent(SearchResultsActivity.this, HomeActivity.class);
                startActivity(intent);
                overridePendingTransition(R.anim.slide_in_from_left, R.anim.slide_out_from_right);
                finish();
            }
        });

        // 初始化搜索结果列表和适配器
        this.searchResults = new ArrayList<>();
        this.adapter = new ArrayAdapter<>(this, android.R.layout.simple_list_item_1, searchResults);
        this.lv_search_results.setAdapter(adapter);

        //文章列表信息处理
        //这种实例化一定要放在onCreate之后！！
        this.searchDetails = new DatabaseHelperForSearchDetails(this).querySearchDataBase(); //读取数据库
        rv_search_details = findViewById(R.id.rv_search_details);
        mMyAdapter = new MyAdapter(searchDetails);
        rv_search_details.setAdapter(mMyAdapter); //设置列表的适配器
        LinearLayoutManager layoutManager = new LinearLayoutManager(SearchResultsActivity.this);
        rv_search_details.setLayoutManager(layoutManager);

        //搜索结果点击事件

    }
    private TextWatcher textWatcher = new TextWatcher() {
        @Override
        public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {
        }

        @Override
        public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {
            if (!charSequence.toString().isEmpty()) {
                // EditText 不为空，显示搜索结果列表，隐藏<!-- 历史搜索/搜索发现 -->
                lv_search_results.setVisibility(View.VISIBLE);
                rv_search_details.setVisibility(View.GONE);
            } else {
                // EditText 为空，直接返回搜索界面
                lv_search_results.setVisibility(View.GONE);
                rv_search_details.setVisibility(View.VISIBLE);
                //调用搜索详情界面
                Intent intent=new Intent(SearchResultsActivity.this, SearchActivity.class);
                startActivity(intent);
                overridePendingTransition(0, 0);//取消切换动画
                finish();
            }
            search(charSequence.toString());
        }

        @Override
        public void afterTextChanged(Editable editable) {
        }
    };
    public void search(String query) {
        // 清空搜索结果列表
        searchResults.clear();
        // 执行搜索逻辑，这里假设是从数据库或其他数据源搜索
        // 例如，你可以在这里执行一个异步任务来获取搜索结果

        // 假设找到了匹配项
        List<String> searchHistory = new DatabaseHelperForSearchResult(this).querySearchDataBase(query);
        searchResults.addAll(searchHistory);

        // 更新适配器和ListView
        adapter.notifyDataSetChanged();
    }

    @Override
    protected void onResume() {
        super.onResume();
        et_search_text.addTextChangedListener(textWatcher);
    }

    class MyAdapter extends RecyclerView.Adapter<MyViewHoder> {
        private List<ArticleInfo> list;
        private View inflater;

        public MyAdapter(List<ArticleInfo> list) {
            this.list = list;
        }

        @NonNull
        @Override
        public MyViewHoder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
            View view = View.inflate(SearchResultsActivity.this, R.layout.article_item, null);
            MyViewHoder myViewHoder = new MyViewHoder(view);

            //调用文章详情页面
            myViewHoder.itemView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    int position = myViewHoder.getAdapterPosition();
                    //Toast.makeText(view.getContext(),"点击了："+ list.get(position).mtv_title, Toast.LENGTH_SHORT).show();
                    Intent intent=new Intent(SearchResultsActivity.this, ArticleDetailsActivity.class);
                    //intent传递参数
                    intent.putExtra("mtv_title", list.get(position).mtv_title);
                    intent.putExtra("usrImage", list.get(position).usrImage);
                    intent.putExtra("mtv_author", list.get(position).mtv_author);
                    intent.putExtra("mtv_summary", list.get(position).mtv_summary);
                    intent.putExtra("contentImage", list.get(position).contentImage);
                    intent.putExtra("mtv_content", list.get(position).mtv_content);
                    intent.putExtra("mtv_data_read", list.get(position).mtv_data_read);
                    intent.putExtra("mtv_data_like", list.get(position).mtv_data_like);
                    intent.putExtra("mtv_data_star", list.get(position).mtv_data_star);
                    startActivity(intent);
                    overridePendingTransition(R.anim.slide_in_from_right, R.anim.slide_out_from_left);
                }
            });
            return myViewHoder;
        }

        @Override
        public void onBindViewHolder(@NonNull MyViewHoder holder, int position) {
            ArticleInfo news = searchDetails.get(position);
            holder.mtv_title.setText(news.mtv_title);
            holder.usrImage.setImageURL(news.usrImage);
            holder.mtv_author.setText(news.mtv_author);
            holder.mtv_summary.setText(news.mtv_summary);
            holder.mtv_data_read.setText(news.mtv_data_read + "阅读");
        }

        @Override
        public int getItemCount() {
            return searchDetails.size();
        }
    }

    class MyViewHoder extends RecyclerView.ViewHolder {
        TextView mtv_title;
        MyImageView usrImage;
        TextView mtv_author;
        TextView mtv_summary;
        TextView mtv_data_read;

        public MyViewHoder(@NonNull View itemView) {
            super(itemView);
            mtv_title = itemView.findViewById(R.id.tv_title);
            usrImage = itemView.findViewById(R.id.myiv);
            mtv_author = itemView.findViewById(R.id.tv_author);
            mtv_summary = itemView.findViewById(R.id.tv_summary);
            mtv_data_read = itemView.findViewById(R.id.tv_data);
        }
    }
}
